package org.jeecg.common.util;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.krysalis.barcode4j.impl.code39.Code39Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
public class BarCodeUtils {

	/**
	 * @description 生成条形码图片的方法
	 * @author heckerstone
	 * @date 2020-08-11
	 * @param targetCode  需要生成的条码
	 * @param path 生成位置  该位置需要带着生成图片的文件名，如：/Users/mac/7.png
	 * @return 生成结果 成功 true，否则false
	 */
	public static boolean generateBarCode(String targetCode,String path) {
		boolean flag=false;
		int height = 160;
		double width = UnitConv.in2mm(1.0f / height);
		File file = new File(path);
		OutputStream out;
		try {
			out = new FileOutputStream(file);
		} catch (FileNotFoundException e) {
			throw new RuntimeException(e);
		}
		Code39Bean c39 = new Code39Bean();
		// 设置属性
		c39.setModuleWidth(width);
		c39.setWideFactor(2);
		String format = "image/png";
		BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, format, height, BufferedImage.TYPE_BYTE_BINARY,false, 0);
		// 生成图片
		c39.generateBarcode(canvas, targetCode);
		try {
			// 关闭流
			canvas.finish();
			flag=true;
		} catch (IOException e) {
//			throw new RuntimeException(e);
			flag=false;
		}
		return flag;
	}
	
	/**
	 * @Description 根据系统的下载相对路径完成条码生成
	 * @param targetCode
	 * @param path
	 * @return
	 * Create at: 2020-08-17 17:17:36
	 * @author: duanyong
	 * Revision:
	 *    2020-08-17 17:17:36 - first revision by duanyong
	 *        
	 */
	public static boolean generateLocalBarCode(String targetCode, String path, String fileName) {
		boolean flag=false;
		int height = 160;
		double width = UnitConv.in2mm(1.0f / height);
		File folderFile = new File(path);
		if (!folderFile.exists()) {
			folderFile.mkdirs();
		}
		fileName = StrUtil.replace(fileName, "/", "");
		fileName = StrUtil.replace(fileName, "\\", "");
		File file = new File(path + File.separator + fileName);
		OutputStream out;
		try {
			out = new FileOutputStream(file);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		Code39Bean c39 = new Code39Bean();
		// 设置属性
		c39.setModuleWidth(width);
		c39.setWideFactor(2);
		String format = "image/png";
		BitmapCanvasProvider canvas = new BitmapCanvasProvider(out, format, height, BufferedImage.TYPE_BYTE_BINARY,false, 0);
		// 生成图片
		c39.generateBarcode(canvas, targetCode);
		try {
			// 关闭流
			canvas.finish();
			flag=true;
		} catch (IOException e) {
//			throw new RuntimeException(e);
			flag=false;
		}
		return flag;
	}
	
   public static void main(String[] args) {
//	   boolean flag=BarCodeUtils.generateBarCode("R04-Regalite-R1010-202000-0145", "D:/122.png");
//	   if(flag) {
//		   System.out.println("生成条码成功！");
//	   }else{
//		   System.out.println("生成条码失败！");
//	   }
	   
	   QrCodeUtil.generate("PA-0111-210415-1026", 300, 300, FileUtil.file("d:/sssss1265.png"));
	   
   }

}
